package com.hxd.otc.module.wallet.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.FrameLayout;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.other.MyCaptureFragment;
import com.hxd.otc.widget.TitleBar;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;

import java.util.List;

import butterknife.BindView;

/**
 * 作  者 :  卫广玉
 * 时  间 :  2018/6/7 15:53
 * 描  述 :  默认扫描页面
 */
public class CommonCaptureActivity extends BaseActivity {

    @BindView(R.id.fl_my_container)
    FrameLayout flMyContainer;
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    private MyCaptureFragment captureFragment;

    private static final int PICK_IMAGE_REQUEST_CODE = 150;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet_common_capture;
    }

    @Override
    protected void initView() {
        captureFragment = new MyCaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.view_common_camera);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();

        titleBar.getRightText().setText(R.string.wallet_qr_album);
        titleBar.getRightText().setTextColor(getResources().getColor(R.color.color_FF464E5F));

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        titleBar.getRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Matisse.from(CommonCaptureActivity.this)
                        .choose(MimeType.ofImage())
                        .countable(true)
                        .maxSelectable(1)
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        .thumbnailScale(0.85f)
                        .imageEngine(new GlideEngine())
                        .forResult(PICK_IMAGE_REQUEST_CODE);
            }
        });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);
            CommonCaptureActivity.this.setResult(RESULT_OK, resultIntent);
            CommonCaptureActivity.this.finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            CommonCaptureActivity.this.setResult(RESULT_OK, resultIntent);
            CommonCaptureActivity.this.finish();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /**
         * 选择系统图片并解析
         */
        if (requestCode == PICK_IMAGE_REQUEST_CODE) {
            if (data != null) {
                List<String> filePaths = Matisse.obtainPathResult(data);
                if (filePaths != null && filePaths.size() > 0) {
                    try {
                        CodeUtils.analyzeBitmap(filePaths.get(0), new CodeUtils.AnalyzeCallback() {
                            @Override
                            public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                                Intent resultIntent = new Intent();
                                Bundle bundle = new Bundle();
                                bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                                bundle.putString(CodeUtils.RESULT_STRING, result);
                                resultIntent.putExtras(bundle);
                                CommonCaptureActivity.this.setResult(RESULT_OK, resultIntent);
                                CommonCaptureActivity.this.finish();
                            }

                            @Override
                            public void onAnalyzeFailed() {
                                showToast(getString(R.string.wallet_error_qr));
                                Handler handler = captureFragment.getHandler();
                                Message msg = Message.obtain();
                                msg.what = R.id.restart_preview;
                                handler.sendMessageDelayed(msg, 2000);
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }
}
